#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cstring>

using namespace std;

class Solution
{
public:
    vector<string> validateCoupons(vector<string> &code, vector<string> &businessLine, vector<bool> &isActive)
    {
        int n = isActive.size();
        vector<int> index;
        vector<string> bsAll = {"electronics", "grocery", "pharmacy", "restaurant"};

        for (int i = 0; i < n; ++i)
        {
            if (code[i].empty() || !isActive[i] || find(bsAll.begin(), bsAll.end(), businessLine[i]) == bsAll.end())
            {
                continue;
            }

            // 验证 code 仅由字母数字字符和下划线组成
            bool codeIsValid = true;
            for (char c : code[i])
            {
                if (!isalnum(c) && c != '_')
                {
                    codeIsValid = false;
                    break;
                }
            }
            if (!codeIsValid)
            {
                continue;
            }

            index.push_back(i);
        }

        if (index.empty())
            return {};

        // 排序
        sort(index.begin(), index.end(),
             [&](const int &ix, const int &iy)
             {
                 auto it_x = find(bsAll.begin(), bsAll.end(), businessLine[ix]);
                 auto it_y = find(bsAll.begin(), bsAll.end(), businessLine[iy]);

                 if (it_x != it_y)
                 {
                     return it_x < it_y;
                 }
                 return code[ix] < code[iy];
             });

        vector<string> result;
        for (int i : index)
        {
            result.push_back(code[i]);
        }

        return result;
    }
};

int main()
{

    // system("pause");
    return 0;
}
